Unity 的 HTTP 网络通信
使用的工具类
Unity 中 UnityWebRequest 和 WWW 都可以实现 HTTP 请求,不过后者已经被标上过时了
Get 请求
IEnumerator GetRequest(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
Debug.Log(webRequest.downloadHandler.text);
}
}
}
可以改造一下上面的请求,通过回调函数来执行结果
public class NetworkTool
{
private const string Url = "http://localhost:8683";
public delegate void RequestCallback(string result);
public static IEnumerator GetRequest(string path, RequestCallback callback)
{
using (var webRequest = UnityWebRequest.Get(Url + path))
{
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
callback( webRequest.downloadHandler.text);
}
}
}
}
下载图片
public class UNityWebTest : MonoBehaviour
{
string url = @"http://pic37.nipic.com/20140113/8800276_184927469000_2.png";
void Start()
{
StartCoroutine(DownSprite());
StartCoroutine(DownMaterial());
}
IEnumerator DownSprite()
{
UnityWebRequest wr = new UnityWebRequest(url);
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
wr.downloadHandler = texDl;
yield return wr.SendWebRequest();
int width = 1920;
int high = 1080;
if (!wr.isNetworkError)
{
tex = texDl.texture;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
transform.GetComponent<Image>().sprite = sprite;
}
}
IEnumerator DownMaterial()
{
UnityWebRequest wr = new UnityWebRequest(url);
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
wr.downloadHandler = texDl;
yield return wr.SendWebRequest();
if (!wr.isNetworkError)
{
Texture2D tex = null;
tex = texDl.texture;
GameObject.Find("Cube").GetComponent<Renderer>().material.mainTexture = tex;
}
}
private void OnApplicationQuit()
{
StopAllCoroutines();
}
}
Post 请求
提交表单
IEnumerator PostRequest(string url)
{
WWWForm form = new WWWForm();
form.AddField("name", "posttest");
using (UnityWebRequest webRequest = UnityWebRequest.Post(url, form))
{
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
Debug.Log(webRequest.downloadHandler.text);
}
}
}
Post 请求需要用到 WWWForm 来添加 post 数据,WWWForm.AddBinaryData 还可以添加二进制数据(如上传文件)。
也可以直接传入一个 Dictionary<string, string>
的字典,作用同 WWWForm
发送 JSON 数据
这里使用回调函数,方便请求成功后执行相应的方法
private const string Url = "http://localhost:8683";
public delegate void RequestCallback(string result);
/// <summary>
/// Post 请求,发送 JSON
/// </summary>
/// <param name="path">请求地址</param>
/// <param name="json">发送的 json 数据</param>
/// <param name="callback">回调函数</param>
/// <returns></returns>
public static IEnumerator PostJsonRequest(
string path,
string json,
RequestCallback callback)
{
// 默认的 POST 请求是 application/x-www-form-urlencoded 所以这里不能直接使用 POST 方法
using (var webRequest = new UnityWebRequest(Url + path, UnityWebRequest.kHttpVerbPOST))
{
webRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
webRequest.downloadHandler = new DownloadHandlerBuffer();
webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
yield return webRequest.SendWebRequest();
if (!string.IsNullOrEmpty(webRequest.error))
{
Debug.LogError(webRequest.error);
}
else
{
callback(webRequest.downloadHandler.text);
}
}
}
Reference
参考资料 unity实现HTTP请求 参考资料 UnityWebRequest 加载网络图片当作贴图给物体